package com.ihd.ihardware.view.tzc.me.view;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.ActivityDeviceBinding;
import com.ihd.ihardware.pojo.DeviceListRes;
import com.ihd.ihardware.view.tzc.me.viewmodel.DeviceViewModel;

import java.util.List;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.widget.MDialog;

public class DeviceActivity extends BaseActivity<ActivityDeviceBinding, DeviceViewModel> {

    private MDialog alert;
    private DeviceListRes.DataBean.ListBean ddl;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(false).init();
        binding.mtitlebar.setTitle("智能秤");
        binding.mtitlebar.setTitleColor(getResources().getColor(R.color.C_FFFFFF));
        binding.mtitlebar.setLeftBack(this);
        binding.mtitlebar.setLeftImageResource(R.drawable.ic_back_w);

        ddl = (DeviceListRes.DataBean.ListBean) getIntent().getSerializableExtra("device");
        binding.snTV.setText(ddl.getMac());

        setRx(AppConstans.UNBIND, new BaseConsumer<DeviceListRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(DeviceListRes mLiveData) {
                DialogUtils.successDialog(DeviceActivity.this, MDialog.DG_TYPE.SUCCESS, R.drawable.toasticon_suc, "解除成功");
                SPUtils.putString(AppConstans.DEVICES, new Gson().toJson(mLiveData.getData().getList(), new TypeToken<List<DeviceListRes.DataBean.ListBean>>() {
                }.getType()));
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 2100);
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        binding.unbind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert = DialogUtils.alertDialog(DeviceActivity.this, MDialog.DG_TYPE.ALERT, "确认解除绑定?", "取消", "确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alert.cancel();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alert.cancel();
                        viewModel.unbind(ddl.getId());
                    }
                });
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device;
    }

    @Override
    protected Class getViewModelClass() {
        return DeviceViewModel.class;
    }

}
